package gui;

import gfx.Drawable;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import core.Main;
import core.Vector2;

public class DropDownMenu implements Drawable{
	private int posX;
	private int posY;
	private int selected;
	private int activeSubMenu;
	private boolean draw;
	private ArrayList<DropDownMenuObj> menu;
	
	private Image menuTile;
	private Image menuTileSub;
	private Image menuTileSel;
	private Image menuTileSubSel;
	
	public static final int DD_PRESET_X = 2;
	public static final int DD_PRESET_Y = 1;
	
	public DropDownMenu(){
		Main.addDrawable(this);
		
		selected = -1;
		activeSubMenu = -1;
		draw = false;
		
		menu = new ArrayList<DropDownMenuObj>();
		
		menuTile = Main.imageLoader.load("/gui/ddmenu.png");
		menuTileSub = Main.imageLoader.load("/gui/ddmenusub.png");
		menuTileSel = Main.imageLoader.load("/gui/ddmenusel.png");
		menuTileSubSel = Main.imageLoader.load("/gui/ddmenusubsel.png");
	}
	
	public void open(int x, int y){
		posX = x;
		posY = y;
		draw = true;
	}
	
	public void close(){
		draw = false;
	}
	
	public void select(int x, int y){
		if(activeSubMenu >= 0 && menu.get(activeSubMenu) instanceof Submenu){
			((Submenu) menu.get(activeSubMenu)).getSubMenu().select(x, y);
			if(((Submenu) menu.get(activeSubMenu)).getSubMenu().getSelected() == -1){
				((Submenu) menu.get(activeSubMenu)).getSubMenu().close();
			}
		}
		if(draw && x >= posX && x <= (posX + 128) && y >= posY && y <= posY + menu.size() * 16){
			selected = (y - posY - 1) / 16;
		}else{
			selected = -1;
		}
		if(activeSubMenu >= 0 && draw && menu.size() > 0){
			if(menu.get(activeSubMenu) instanceof Submenu){
				((Submenu) menu.get(activeSubMenu)).getSubMenu().open(posX + 128, posY + selected * 16);
				activeSubMenu = selected;
			}
		}
	}
	
	public void trigger(){
		if(selected >= 0){
			if(!(menu.get(selected) instanceof Submenu)){
				draw = false;
			}else{
				((Submenu)menu.get(selected)).getSubMenu().open(posX + 128, posY);
			}
			menu.get(selected).trigger();
		}else if(activeSubMenu == -1){
			close();
		}
	}
	
	public void addToMenu(DropDownMenuObj obj){
		menu.add(obj);
	}

	@Override
	public void render(Graphics g) {
		if(draw){
			g.setFont(GUI.ddFont);
			g.setColor(Color.WHITE);
			
			for(int i = 0;i < menu.size();i++){
				if(!(menu.get(i) instanceof Submenu)){
					if(selected >= 0 && i == selected){
						g.drawImage(menuTileSel, posX, posY + selected * 16, null);
					}else{
						g.drawImage(menuTile, posX, posY + i * 16, null);
					}
					menu.get(i).draw(g, posX + DD_PRESET_X, posY + DD_PRESET_Y + i * 16, 128, 16);
				}else{
					if(selected >= 0 && i == selected){
						g.drawImage(menuTileSubSel, posX, posY + selected * 16, null);
					}else{
						g.drawImage(menuTileSub, posX, posY + i * 16, null);
					}
					menu.get(i).draw(g, posX + DD_PRESET_X, posY + DD_PRESET_Y + i * 16, 128, 16);
				}
			}
		}
	}
	
	public int getX(){
		return posX;
	}
	
	public int getY(){
		return posY;
	}
	
	public int getSelected(){
		return selected;
	}

	@Override
	public boolean removed() {
		return false;
	}

	@Override
	public Vector2 getPos() {
		return null;
	}

	@Override
	public Color getColor() {
		return null;
	}

	@Override
	public Vector2 getSize() {
		return null;
	}
}
